package vinylInt 
{
	//confing
	import vinylInt.VinylIntISConfiguration;
	
	//deeplink
	import vinylInt.VinylIntSwfAddress;
	
	import com.vinyl.app.Application;
	import com.vinyl.app.Activity;
	import com.vinyl.app.Timeline;
	import com.vinyl.command.Call;
	import com.vinyl.controls.*;
	
	//data
	import com.vinyl.events.DataService;
	import com.vinyl.events.DataEvent;

	//sound & media share
	import com.vinyl.sound.SoundLoader;
	import com.vinyl.sound.SoundProxy;
	import com.vinyl.sound.SoundService;
	import com.vinyl.sound.SoundPlayer;
    import flash.media.Sound;
	
	//font & TextField share
	import com.vinyl.font.FontProxy;
	import com.vinyl.font.FontService;
	import com.vinyl.components.EmbedFontSimpleLable;
	import flash.text.Font;
	import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
    import flash.text.TextFormat;
	
	//internal
	import flash.system.ApplicationDomain;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	import flash.utils.clearInterval;
	
	/**
	 * ...
	 * @author 
	 */
	public class Sub_2 extends Application
	{
		//딥링크 오브젝트
		private var _swfAddress:VinylIntSwfAddress;
		//환경 설정
		private var _con:VinylIntISConfiguration;
		//공유 객체 라이브러리 도메인
		private var _ad:ApplicationDomain;
		//에니메이션 엑티버트 오브젝트
		private var _activity:Activity;
		private var _activityMc:MovieClip;
		//데이터 서비스 오브젝트
		private var _service:DataService;
		
		//sound
		private var _mp3Player; //사운드 플레이어
		private var _sound_00; //사운드 배경...
		private var _sound_01; //사운드 효과 1...
		private var _sound_02; //사운드 효과 2...
		private var _sound_03; //사운드 효과 3...
		private var _sound_04; //사운드 효과 4...
		
		private var _nc:MovieClip; //실행중인 무비클립
		private var _idd:Number; //setTimeout
		
		private var output;
		private var output2;
		
		public function Sub_2() 
		{
			addEventListener( Event.ADDED_TO_STAGE, config );
		}
		
		private function config(e)
		{
			//환경설정
			_con = VinylIntISConfiguration.getInstance();
			_con.addEventListener( Event.COMPLETE, function(e) { 
				_con.removeEventListener( Event.COMPLETE, arguments.callee );
				//초기화
				init();
			} );
			
			_con.config( this, "configuration/config.ini"  );
		}
		
		private function init()
		{
			//라이브러리 공유 및 로드
			if ( ( parent as Object ).root == stage )
			{
				//공유할 객체를 직접 가져오기
				var $loader:Loader = new Loader(); 
				var $request:URLRequest = new URLRequest( "shared/vinyl int components.swf" ); 
				$loader.load( $request );
				$loader.contentLoaderInfo.addEventListener( Event.COMPLETE, $completeSharedListener );
				
				function $completeSharedListener( $event:Event ):void 
				{
					//리스너 재거
					$event.target.removeEventListener( Event.COMPLETE, $completeSharedListener );
					_ad = $event.target.applicationDomain;
					
					configUI();
				}
			}
			else
			{
				configUI();
			}
			
			//딥링크 설정
			_swfAddress = VinylIntSwfAddress.getInstance();
			SWFAddress.addEventListener( SWFAddressEvent.CHANGE, handleSWFAddress );
			
			//에니메이션을 위한 엑티버티
			_activityMc = new MovieClip();
			_activity = new Activity( _activityMc );
			
			//xml데이터 가져오기
			_service = DataService.getInstance();
			
			//사운드 로드 및 재생
			_mp3Player = SoundPlayer.getInstance();
			
			var soundService = SoundService.getInstance();
			_sound_00 = soundService.loadSound( "sound/bgm.mp3" );
			_sound_01 = soundService.loadSound( "sound/tic.mp3" );
			_sound_02 = soundService.loadSound( "sound/ting.mp3" );
			_sound_03 = soundService.loadSound( "sound/tock.mp3" );
			_sound_04 = soundService.loadSound( "sound/tuck.mp3" );
			_sound_00.addEventListener( com.vinyl.events.DataEvent.GETDATA, function(e) {
				_sound_00.removeEventListener( com.vinyl.events.DataEvent.GETDATA, arguments.callee );
				_mp3Player.soundPlayBG( _sound_00.sound, .1, 0 );
			});
			_sound_00.load();
			_sound_01.load();
			_sound_02.load();
			_sound_03.load();
			_sound_04.load();
			
			//공유폰트 & 텍스트 필드
			var fontService = FontService.getInstance();
			var font_00 = fontService.getFont( "shared/@sharedFont.swf" );
			var font_01 = fontService.getFont( "shared/@sharedFont.swf" );
			font_00.addEventListener( com.vinyl.events.DataEvent.GETDATA, function(e) { 
				trace( e.data.url ); 
				
				//시스템 폰트 사용
				var format = new TextFormat();
				format.color = 0x896D4D;
				format.bold = true;
				format.size = 9;
				format.font = "Verdana";
				
				output = new EmbedFontSimpleLable( new flash.text.Font(), format );
				output.restrict = null;
				output.type = TextFieldType.DYNAMIC;
				output.selectable = true;
				output.multiline = true;
				output.width = 200;
				output.height = 200;
				output.embedFonts = false;
				output.x = 10;
				output.y = 200;
				output.autoSize = flash.text.TextFieldAutoSize.LEFT 
				output.text = "Vinyl Int sub_2";
				addChild( output );
				
				//커스텀 임베디드 폰트 사용
				var embeddedFonts:Array = Font.enumerateFonts(false);
				embeddedFonts.sortOn("fontName", Array.CASEINSENSITIVE);
				
				format = new TextFormat();
				format.color = 0x333333;
				format.size = 10;
				
				output2 = new EmbedFontSimpleLable( embeddedFonts[0], format );
				output2.width = 200;
				output2.height = 100;
				output2.multiline = true;
				output2.x = 10;
				output2.y = 210;
				output2.autoSize = flash.text.TextFieldAutoSize.LEFT;
				output2.wordWrap = true;
				output2.text = "VINYL I IDA TEAM \n" + "ActionScript Development Policy";
				addChild( output2 );
			} );
			
			font_01.addEventListener( com.vinyl.events.DataEvent.GETDATA, function(e) { 
				trace( e.data.url ); 
			} );
			font_00.load();
			font_01.load();
		}
		
		private function configUI()
		{
			//슬라이더 붙이기
			var ref = getShredObject( "com.vinyl.components.SimpleVSlider" ) as Class;
			var vslider = new ref( 1024 - 12, 0 );
			vslider.way = 768;
			vslider.knobSize( 100 );
			
			addChild( vslider );
			
			
			//슬라이더 붙이기
			ref = getShredObject( "com.vinyl.components.SimpleHSlider" ) as Class;
			var hslider = new ref( 0, 768-12 );
			hslider.way = 1024;
			hslider.knobSize( 100 );
			
			addChild( hslider );
		}
		
		/*
		 * 버튼 클릭 핸들러
		 */
		public function button_click(e)
		{	
			_mp3Player.soundPlay( _sound_01.sound, .2, 0 );
			_swfAddress.btnClick( e );
		}
		
		/*
		 * 공유 라이브러리 객체 얻기
		 */
		private function getShredObject( className:String ): Class
		{
			if ( ( parent as Object ).root == stage )
			{
				return _ad.getDefinition( className ) as Class;
			}
			else
			{
				return ApplicationDomain.currentDomain.getDefinition( className ) as Class;
			}
		}
		
		/*
		 * SWFAddress handling
		 */ 
		private function handleSWFAddress(e:SWFAddressEvent) 
		{
			//현제 실행중인 컨텐츠가 달려있다
			if( _nc != null && container.contains( _nc ) ) 
			{					
				//현제 컨텐츠를 아웃 시키고 제거한다.
				_nc.play();
				
				//e.stopPropagation();
				_idd = setTimeout( handleSWFAddress, 10, e );
				
				return;
			}
			else
			{
				if ( _idd != 0 ) clearInterval( _idd );
				_idd = 0;
			}
			
			if ( _idd > 0 ) return;
			
			product1.y = 82;
			product2.y = 82;
			product3.y = 82;
			
			if ( VinylIntSwfAddress.handle ) VinylIntSwfAddress.handle( e );
			switch( e.value )
			{
				case "/sub_2/1/":
					product1.y = 0;
					
					var c = new Content_0();
					
					setTimeout( function(){
						container.addChild( c );
						c.x = 0;
						c.y = 0;
					}, 10 );
					_nc = c;
					
				break;
				
				case "/sub_2/2/":
					product2.y = 0;
					
					c = new Content_1();
					
					setTimeout( function(){
						container.addChild( c );
						c.x = 0;
						c.y = 0;
					}, 10 );
					_nc = c;
					
				break;
				
				case "/sub_2/3/":
					product3.y = 0;
					
					c = new Content_2();
					
					setTimeout( function(){
						container.addChild( c );
						c.x = 0;
						c.y = 0;
					}, 10 );
					_nc = c;
					
				break;
				
				case "/sub_2/4/":
					_service.addEventListener( com.vinyl.events.DataEvent.GETDATA, function(e) { 
						_service.removeEventListener( com.vinyl.events.DataEvent.GETDATA, arguments.callee );
						trace( e.data );
					} );
					
					_service.addEventListener( "error", function(e) { 
						_service.removeEventListener( "error", arguments.callee );
						
						output2.text = e.data.text;
						output2.width = 250;
						output2.height = 100;
						output2.x = 40;
						output2.y = 70;
						output2.wordWrap = true;
						output2.mouseEnabled = false;
						//output2.mouseChildren = false;
						getShredObject( "com.vinyl.controls.Alert" ).show( ( root as Object ), null, output2, 7, 0, null, null, null,  function( modal: MovieClip, o:Object )
						{ 	
							
							//사용자 정의 콜백핸들러
							modal.x = stage.stageWidth / 2 - 360 / 2 - ( stage.stageWidth - 1024 ) / 2;
							modal.y = stage.stageHeight / 2 - 360 / 2 - ( stage.stageHeight - 768 ) / 2;   
						} ); 
						
						trace( "err" + e.data.text );
					} );
					
					_service.getXML( "xml/product.xml" );
					
				break;
				
				case "":
				break;
			}  
			
			var title:String = 'VINYL Website';	
			
			for (var i = 0; i < e.pathNames.length; i++) {
				title += ' / ' + e.pathNames[i].substr(0,1).toUpperCase() + e.pathNames[i].substr(1);
			}
			
			SWFAddress.setTitle(title);
			VinylIntSwfAddress.tmp = e.pathNames;
		}
	}
}